package com.witframework.optimize;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class TraceFoot {
	private String target="";
	private List<Foot> footmarks;
	private Foot autoNavigateFoot;
	
	private static Map<String,TraceFoot> instanceMap=Collections.synchronizedMap(new HashMap<String,TraceFoot>());
	
	public static TraceFoot getInstance(String username){
		TraceFoot instance=instanceMap.get(username);
		if(instance==null){
			instance=new TraceFoot();
			instanceMap.put(username,instance);
		}
		return instance;
	}
	private TraceFoot(){
		footmarks = Collections.synchronizedList(new ArrayList<Foot>());		
	}
	
	public void addFootmark(String id,String name,String url){
		addTrace(footmarks,id,name,url);		
	}
	public String[] getFootmark(String id){
		for(Foot foot:footmarks){			
			if(foot.id.equalsIgnoreCase(id)){
				return new String[]{foot.name,foot.url};
			}
		}
		return null;
	}
	public void addNavigateTrace(String id,String name,String url){
		addTrace(footmarks,id,name,url);
		if(autoNavigateFoot!=null&&autoNavigateFoot.id.equals(id))
			autoNavigateFoot.counter++;
		else
			autoNavigateFoot=new Foot(id,name,url,1);
	}
	private void addTrace(List<Foot> foots,String id,String name,String url){
		boolean has=false;			
		for(Foot foot:foots){			
			if(foot.id.equalsIgnoreCase(id)){
				foot.counter++;
				has=true;				
				break;
			}
		}	
		if(!has){
			Foot foot=new Foot(id,name,url,1);
			foots.add(foot);			
		}		
	}
	public String getAutoNavigateUrl(String id){
		if(autoNavigateFoot!=null&&autoNavigateFoot.id.equals(id)&&autoNavigateFoot.counter>=3)
			return autoNavigateFoot.url;			
		return null;
	}
	public String getFootmarkAndTrace(int arg0,String icon){	
		StringBuffer trace1=new StringBuffer("");		
		StringBuffer trace2=new StringBuffer("");		
		Foot[] foots=footmarks.toArray(new Foot[0]);
		java.util.Arrays.sort(foots,new FootComparator());
		Foot[] views1=java.util.Arrays.copyOf(foots, arg0);
		java.util.Arrays.sort(foots,new FootComparator2());
		Foot[] views2=java.util.Arrays.copyOf(foots, arg0);
		for(int i=0;i<views1.length;i++){
			Foot foot=views1[i];
			if(foot!=null)
				trace1.append((icon!=null?"<img class=\"img16\" src=\""+icon+"\"/>":"")+"<a href='"+foot.url+"' target='"+target+"'>"+ foot.name+"</a>");
			Foot foot2=views2[i];
			if(!java.util.Arrays.asList(views1).contains(foot2)&&foot2!=null)
				trace2.append((icon!=null?"<img class=\"img16\" src=\""+icon+"\"/>":"")+"<a href='"+foot2.url+"' target='"+target+"'>"+ foot2.name+"</a>");
		}
		return "<table><tr><td align=left>"+trace1.toString()+"</td><td align=right>"+trace2.toString()+"</td></tr></table>";
	}
	public void setTarget(String _target){
		target=_target;
	}
	class Foot{
		String id;
		String name;
		String url;
		int counter;
		long lastaccess;
		public Foot(String id,String name,String url,int counter){
			this.id=id;
			this.name=name;
			this.url=url;
			this.counter=counter;
			this.lastaccess=new java.util.Date().getTime();
		}
		public void count(){
			this.counter++;
			this.lastaccess=new java.util.Date().getTime();
		}
		@Override
		public boolean equals(Object obj){
			return this.id.equals(((Foot)obj).id)?true:false;
		}
	}
	class FootComparator implements Comparator{
		public int compare(Object o1, Object o2) {		
			return ((Foot)o2).counter-((Foot)o1).counter;
		}
		
	}	
	class FootComparator2 implements Comparator{
		public int compare(Object o1, Object o2) {
			long now=new java.util.Date().getTime();
			Foot f1=(Foot)o1;
			Foot f2=(Foot)o2;			
			return (int)(f2.lastaccess-f1.lastaccess);
		}		
	}	
	
}
